﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
using StructureMap;
using TimeTracker.Core.Domain;
using TimeTracker.Core.Domain.Entities;

namespace TimeTracker.Core.DataAccess.Implementation
{
    [Pluggable("Default")]
    public class RadnikRepository : RepositoryBase, IRadnikRepository
    {
        public List<Radnik> UzmiSveRadnike()
        {
            var radniciQuery = (from radnici in NHibernateSession.Linq<Radnik>()
                                select radnici).ToList();

            return radniciQuery;
        }

        public List<Radnik> UzmiRadnikePoOrganizacionojSemi(int orgSemaId)
        {
            var radniciQuery = (from radnici in NHibernateSession.Linq<Radnik>()
                                where radnici.OrganizacionaSema.OrganizacionaSemaId == orgSemaId
                                select radnici).ToList();

            return radniciQuery;
        }

        public Radnik UzmiRadnikaPoId(int radnikId)
        {
            var radnik = (from radnici in NHibernateSession.Linq<Radnik>()
                                where radnici.RadnikId == radnikId
                                select radnici).SingleOrDefault();

            return radnik;
        }

        public void DodajRadnika(string ime, string prezime, string email, int orgSemaId)
        {
            var sema = (from seme in NHibernateSession.Linq<OrganizacionaSema>()
                        where seme.OrganizacionaSemaId == orgSemaId
                        select seme).SingleOrDefault();


            var radnik = new Radnik();
            radnik.Ime = ime;
            radnik.Prezime = prezime;
            radnik.Email = email;
            radnik.OrganizacionaSema = sema;

            NHibernateSession.SaveOrUpdate(radnik);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }

        public void AzurirajRadnika(Radnik radnik)
        {
            var r = (from radnici in NHibernateSession.Linq<Radnik>()
                          where radnici.RadnikId == radnik.RadnikId
                          select radnici).SingleOrDefault();

            NHibernateSession.SaveOrUpdate(r);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }

        public void AzurirajRadnika(int radnikId, string ime, string prezime, string email, int orgSemaId)
        {
            var radnik = (from radnici in NHibernateSession.Linq<Radnik>()
                     where radnici.RadnikId == radnikId
                     select radnici).SingleOrDefault();

            var sema = (from seme in NHibernateSession.Linq<OrganizacionaSema>()
                        where seme.OrganizacionaSemaId == orgSemaId
                        select seme).SingleOrDefault();

            radnik.Ime = ime;
            radnik.Prezime = prezime;
            radnik.Email = email;
            radnik.OrganizacionaSema = sema;

            NHibernateSession.Update(radnik);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }

        public void ObrisiRadnika(int radnikId)
        {
            var radnik = (from radnici in NHibernateSession.Linq<Radnik>()
                          where radnici.RadnikId == radnikId
                          select radnici).SingleOrDefault();

            NHibernateSession.Delete(radnik);

            using (ITransaction transaction = NHibernateSession.BeginTransaction())
            {
                try
                {
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }

        }
    }
}
